import requests
import http.cookiejar as cookielib
import websocket
import json
try:
    import thread
except ImportError:
    import _thread as thread
import time
def on_message(ws, message):
    if message == '3probe':
        ws.send('5')
    if message == '6':
        ws.send('7')
    if message == '1':
        ws.send('2')
    print(message)
    # print(json.loads(message))


def on_error(ws, error):
    print(error)


def on_close(ws, close_status_code, close_msg):
    print("### closed ###")
    # clientInit()


def on_open(ws):
    def run(*args):
        ws.send('2probe')
        time.sleep(1)
        
        ws.send('427["subscribe",{"channel":"AF820958","sid":"2d2c8e54-8004-433c-8ae2-b0b4f5bb14b7"}]')
        ws.send('428["subscribe",{"channel":"DC65D155","sid":"2d2c8e54-8004-433c-8ae2-b0b4f5bb14b7"}]')
        ws.send('429["subscribe",{"channel":"C77DFE28","sid":"2d2c8e54-8004-433c-8ae2-b0b4f5bb14b7"}]')
        ws.send('4210["subscribe",{"channel":"43150F2B","sid":"2d2c8e54-8004-433c-8ae2-b0b4f5bb14b7"}]')
        ws.send('4211["subscribe",{"channel":"6883CA2F","sid":"2d2c8e54-8004-433c-8ae2-b0b4f5bb14b7"}]')
        
        # ws.close()
        print("thread terminating...")
    thread.start_new_thread(run, ())


def clientInit():
    # websocket.enableTrace(True)
    url = 'wss://hangzhou.goeasy.io'
    ws = websocket.WebSocketApp("wss://6hangzhou.goeasy.io/socket.io/?EIO=3&transport=websocket&sid=960ec37f-f548-4edc-b2a2-4b1239dcb8b5&b64=1",
                                on_open=on_open,
                                on_message=on_message,
                                on_error=on_error,
                                on_close=on_close)
    ws.run_forever()

##登录网站
def login():
    token = '83ab5429ef6445f5918c4126714a65a9'
    header ={
        "X-Forwarded-Host": '1188.zhijiepay.net',
        "token": token,
        "Content-Type": 'application/x-www-form-urlencoded; charset=utf-8',
        }
    login_url = 'https://api-v2.mintaiky.com:8888/request/site/user/log'
    post = {
        'url':'',
        'ghost':''
    }
    mafengwoSession = requests.session()
    mafengwoSession.cookies = cookielib.LWPCookieJar(filename = "bet33.txt")
    r = requests.post(login_url, data=post, headers=header)
    print(r.status_code)
    print(r.text)

    if r.status_code == 200:
        print(r.text)
        header = {
            'origin': 'https://1188.zhijiepay.net',
            'path': '/request/esport/match/list',
            'x-forwarded-host': '1188.zhijiepay.net',
            'token': token,
            'content-type': 'application/x-www-form-urlencoded',
            'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'
        }
        url = 'https://api-v2.mintaiky.com:8888/'
        r = requests.post(url, headers=header)
        print(r.status_code)
        print(r.text)

        # res = json.loads(r.text)


# login()
clientInit()
